const net = require("net");

const server = net.createServer();

const PORT = 1234;
const HOST = "localhost";

server.listen(PORT, HOST);

server.on("listening", () => {
  console.log(`服务器一开始${HOST}:${PORT}`);
});

server.on("connection", (socker) => {
  // 双工可写可读
  socker.on("data", (chunk) => {
    const msg = chunk.toString();
    console.log(msg);
    socker.write(Buffer.from("你好" + msg));
  });
});

server.on("close", () => {
  console.log("服务器关闭");
});
server.on("error", (err) => {
  if (err.code === "EADDRINUSE") {
    console.log("地址使用");
  } else {
    console.log(err);
  }
});
